package com.CGFrame.struts.bean.category;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import com.CGFrame.struts.POJOs.Category;
import com.CGFrame.struts.POJOs.Catemodel;
import com.CGFrame.struts.POJOs.Catemodelrel;
import com.CGFrame.struts.POJOs.Catemodeltime;
import com.CGFrame.struts.POJOs.Company;
import com.CGFrame.struts.POJOs.Element;
import com.CGFrame.struts.POJOs.Make;
import com.CGFrame.struts.POJOs.Makerelation;
import com.CGFrame.struts.POJOs.Shot;
import com.CGFrame.struts.POJOs.Status;
import com.CGFrame.struts.bean.HibernateSessionFactory;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.make.MakeBean;
import com.CGFrame.struts.bean.unit.UnitBean;
	
public class CateModelBean {
	
	private Catemodel catemodel=null;
	
	public CateModelBean(){
		
	}
	
	public CateModelBean(int cateModelId)
	{
			ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
			catemodel = (Catemodel)pop.UniqueQuery("Catemodel","cateModelId,"+cateModelId);
	}
	public CateModelBean(Catemodel cateModel)
	{
		this.catemodel = cateModel;
	}

	/**
	 *  ��ĳ��Unit type id ����this.catemodelģ��	
	 * @param unitType  Element or  Shot
	 * @param unitId    
	 */
	public String addModelToUnit(String unitType ,int unitId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
//		Object obj = new Object();
		String type = catemodel.getCateModelType();
		if (!unitType.equals(type))
		{
			System.out.println("Model Type Error");
			return "ModelType Error";
		}
/*		if ( type.equals("Element") )
		{
			obj = pop.UniqueQuery("Element", "elementId,"+unitId);
		}
		else if ( type.equals("Shot"))
		{
			obj = pop.UniqueQuery("Shot", "shotId,"+unitId);
		}*/
		Iterator ite_Cates = this.getCates().iterator();
		UnitBean unitBean = new UnitBean(type , unitId);
		while(ite_Cates.hasNext())
		{
			Category category = (Category)ite_Cates.next();
			Catemodeltime catemodeltime = (Catemodeltime)pop.UniqueQuery("Catemodeltime", "catemodel.cateModelId,"+catemodel.getCateModelId()+",category.categoryId,"+category.getCategoryId());
			String name = category.getCategoryEnglishName();
			Make make = unitBean.addMake(name, category, catemodeltime.getHours());
			if (make != null)//�����¼�����
			{
				MakeBean makeBean =new MakeBean(make);
				makeBean.addTask(null, unitBean.getUnitName()+"_"+make.getMakeName()+"_Task", "", make.getExceptHours());
			}
/*			if ( unitBean.isExist(category.getCategoryId()) )
			{
//				System.out.println(category.getCategoryName()+" exist");
				continue;  //Unit has existed this category ,skip it
			}
			Make make = new Make();
			make.setCategory(category);
			Catemodeltime catemodeltime = (Catemodeltime)pop.UniqueQuery("Catemodeltime", "catemodel.cateModelId,"+catemodel.getCateModelId()+",category.categoryId,"+category.getCategoryId());
			make.setExceptHours(catemodeltime.getHours());
			make.setMakeName(category.getCategoryName());
			Status status = (Status)pop.UniqueQuery("Status", "statusType,Make,statusName,��¼��");
			make.setStatus(status);
			if (type.equals("Element")) make.setElement( (Element)obj);
			if (type.equals("Shot"))    make.setShot( (Shot)obj );
			pop.saveObject(make);
//			System.out.println("����һ��ID"+make.getMakeId()+"��"+make.getMakeName()+".");
 
 */
		}//    Make��������
  
//////////////�ӹ�ϵ///////////////
		int cateModelId = catemodel.getCateModelId();
		ProjectOperateFactory.closeprojectOperate();  //for session update
		ProjectOperate pop2 = ProjectOperateFactory.getprojectOperate();
		catemodel = (Catemodel)pop2.UniqueQuery("Catemodel","cateModelId,"+cateModelId);
		
		/////////////
		Iterator ite_Rels = catemodel.getCatemodelrels().iterator();
		while (ite_Rels.hasNext())
		{
			Catemodelrel catemodelrel = (Catemodelrel)ite_Rels.next();
			Category category1 = catemodelrel.getCategoryByCategory1id();
			Category category2 = catemodelrel.getCategoryByCategory2id();
			Make make1 = unitBean.getMakeByCategory(category1);
			Make make2 = unitBean.getMakeByCategory(category2);
			MakeBean makeBean = new MakeBean(make1);
			boolean isAdd = makeBean.addMakeRelation(make2);
		}
		return "SUCCESS";
	}
	/**
	 * �ó�categoryId ��ǰ���ռ� Category��
	 * @param categoryId
	 * @return
	 */
	public Vector getPreCates(int categoryId)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Iterator ite_PreCateRels = pop.Query("Catemodelrel", "catemodel.cateModelId,"+catemodel.getCateModelId()+",categoryByCategory2id.categoryId,"+categoryId, null, null).iterator();
		Vector vec_PreCates = new Vector();
		while (ite_PreCateRels.hasNext())
		{
			Catemodelrel catemodelrel = (Catemodelrel)ite_PreCateRels.next();
			vec_PreCates.add( catemodelrel.getCategoryByCategory1id() );
		}
		return vec_PreCates;
	}
	
	/**
	 * �ó�category ��ǰ���ռ� Category��
	 * @param category
	 * @return
	 */
	public Vector getPreCates(Category category)
	{
		int categoryId = category.getCategoryId();
		Vector vec = this.getPreCates(categoryId);
		return vec;
	}
	
	/**
	 * �ж�category1-->category2 �Ӻ��Ƿ���������Ȧͼ��
	 * ��ID�����Ļ����� �����ų��һ��t�ϵĽڵ㡣1���������Ȧͼ����һ��������ࡣ
	 * @param category1Id
	 * @param category2Id
	 * @return
	 */
	public boolean isDCG(int category1Id ,int category2Id)///  ��ID�����Ļ����� �����ų��һ��t�ϵĽڵ㡣1���������Ȧͼ����һ��������ࡣ
	{
		boolean boo_isDcg = false;
		Iterator ite_PreCates = this.getPreCates(category1Id).iterator();
		while(ite_PreCates.hasNext())
		{
			Category preCate = (Category)ite_PreCates.next();
//			System.out.println("preCateId:"+preCate.getCategoryId());
			if (preCate.getCategoryId()==category2Id)
			{
//				System.out.println("��Ȧ");
				boo_isDcg = true;
//				System.out.println("����Ȧͼô? "+boo_isDcg);
				break;
			}
			else
			{
//				System.out.println("��ûȦ");
				boo_isDcg = this.isDCG(preCate.getCategoryId(),category2Id);
//				System.out.println("����Ȧͼô? "+boo_isDcg);
				if(boo_isDcg) break;
			}
		}
//		System.out.println("����Ȧͼô?end "+boo_isDcg);
		return boo_isDcg;
	}
	
	/**
	 * ȡ��this.catemodel���漰������category��
	 * @return
	 */
	public Vector getCates_old()
	{
		
		Iterator ite_rels = catemodel.getCatemodelrels().iterator();
		Vector vec_Cates = new Vector();
		while (ite_rels.hasNext())
		{
			Catemodelrel catemodelrel = (Catemodelrel)ite_rels.next();
			Category category1 = catemodelrel.getCategoryByCategory1id();
			Category category2 = catemodelrel.getCategoryByCategory2id();
			if (! vec_Cates.contains(category1) ) vec_Cates.add(category1);
			if (! vec_Cates.contains(category2) ) vec_Cates.add(category2);
		}
		return vec_Cates;
	}
	public Set getCates()
	{
		LinkedHashSet set = new LinkedHashSet();
		Iterator ite_CateModelTimes = catemodel.getCatemodeltimes().iterator();
		while(ite_CateModelTimes.hasNext())
		{
			 Catemodeltime cMTime = (Catemodeltime)ite_CateModelTimes.next();
			 set.add( cMTime.getCategory() );
		}
		return set;
	}
	
	/**
	 * �ж��Ƿ����category1-->category2��ϵ
	 * @param category1Id
	 * @param category2Id
	 * @return String SUCCESS ��ʾ�ɹ�
	 */
	public boolean isExistRel(int category1Id ,int category2Id)
	{
		Iterator ite_PreCates = this.getPreCates(category2Id).iterator();
		while(ite_PreCates.hasNext())
		{
			Category category =(Category)ite_PreCates.next();
			if ( category.getCategoryId().intValue() == category1Id )
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * ��catemodel����ӹ�ϵcategory1-->category2��
	 * ʧ�ܷ��أ�ͬʱ���ԭ��
	 * @param category1
	 * @param category2
	 * @return
	 */
	public String addCateModelRel(Category category1,Category category2)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Catemodelrel caterel = new Catemodelrel();
		if ( this.isExistRel(category1.getCategoryId(), category2.getCategoryId()) )
		{
			
			return "HasExisted" ;
		}
		if (this.isDCG(category1.getCategoryId(), category2.getCategoryId()))
		{
	//		System.out.println("CategoryRel : DCG");
			return "DCG";
		}
		if (this.isDCG(category2.getCategoryId(), category1.getCategoryId()))
		{
			return "Redundancy";
		}
		caterel.setCatemodel(catemodel);
		caterel.setCategoryByCategory1id(category1);
		caterel.setCategoryByCategory2id(category2);
		pop.saveObject(caterel);
		return "SUCCESS";
	}
	
	public Catemodel getCatemodelById(String modelId){
		ProjectOperate pop=ProjectOperateFactory.getprojectOperate();
		Catemodel model=(Catemodel)pop.UniqueQuery("Catemodel", "cateModelId,"+modelId);
		return model;
	}
	
	public List getModels(){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		List models=pop.Query("Catemodel", null,null,null);
		return models;
	}
	
	public Integer createModel(String companyId,String name,String type,String desc){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Catemodel mod=new Catemodel();
		mod.setCateModelName(name);
		mod.setCateModelType(type);
		mod.setDescription(desc);
		Company c = (Company)pop.UniqueQuery("Company", "companyId,"+companyId);
		mod.setCompany(c);
		pop.saveObject(mod);
		return mod.getCateModelId();
	}
	public void changeCateModel(int id){
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		catemodel = (Catemodel)pop.UniqueQuery("Catemodel","cateModelId,"+id);
	}

	public Catemodel getCatemodel() {
		return catemodel;
	}
}
